package com.lzy.cloud.permission.facade;

import com.lzy.cloud.permission.dtos.ResourceDto;
import com.lzy.cloud.permission.dtos.RoleResourcePermissionDto;
import com.lzy.cloud.permission.dtos.UserRoleDto;
import com.lzy.cloud.permission.entity.Resource;
import com.lzy.cloud.permission.service.ResourceService;
import com.lzy.cloud.permission.service.RoleResourcePermissionService;
import com.lzy.cloud.permission.service.RoleService;
import com.lzy.cloud.permission.service.UserRoleService;
import com.lzy.framework.core.ensure.Ensure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by WIN7 on 2018/5/18.
 */
@Component
public class ResourceFacade {

    @Autowired
    ResourceService resourceService;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    RoleResourcePermissionService roleResourcePermissionService;

    /**
     * 删除资源
     *
     * @param id
     **/
    public Boolean deleteById(Long id) {
        Integer countChild = countChild(null, id, null, null);
        //当前资源存在下级菜单，不予删除
        Ensure.that(countChild > 0).isFalse("00030006");
        //删除角色资源权限关联表
        RoleResourcePermissionDto roleResourcePermissionDto = new RoleResourcePermissionDto();
        roleResourcePermissionDto.setResourceId(id);
        roleResourcePermissionService.delete(roleResourcePermissionDto);
        //删除资源表
        return resourceService.deleteById(id);
    }

    /**
     * 获取该用户的资源 - 一级子节点
     *
     * @param userId   用户id
     * @param parentId 父级资源id
     * @param status   状态
     * @param type     类型
     * @return
     */
    public List<ResourceDto> selectUserResource(Long userId, Long parentId, Integer status, Integer type) {
        Map<String, Object> params = new HashMap<String, Object>();
        String resourceIds = "";
        params.put("resourceIds", null);
        if (userId != null) {
            resourceIds = getResourceIds(userId, resourceIds);
            params.put("resourceIds", resourceIds);
        }
        params.put("type", type);
        params.put("parentId", parentId);
        params.put("status", status);
        List<ResourceDto> resourceDtoList = resourceService.selectByIds(params);
        return resourceDtoList;
    }

    /**
     * 获取该用户的资源 - 所有子节点
     *
     * @param userId   用户id
     * @param parentId 父级资源id
     * @param status   状态
     * @param type     类型
     * @return
     */
    public List<ResourceDto> selectUserAllResource(Long userId, Long parentId, Integer status, Integer type) {
        List<ResourceDto> resourceDtoList = selectUserResource(userId, parentId, type, status);
        if (resourceDtoList == null || resourceDtoList.isEmpty())
            return null;
        for (int i = 0; i < resourceDtoList.size(); i++) {
            resourceDtoList.get(i).setChildren(
                    setChildren(userId, type, resourceDtoList.get(i).getId(), status));
        }
        return resourceDtoList;
    }

    /**
     * 递归
     *
     * @param userId
     * @param type
     * @param parentId
     * @param status
     * @return
     */
    private List<ResourceDto> setChildren(Long userId, Integer type,
                                          Long parentId, Integer status) {
        List<ResourceDto> resourcesDtoList = selectUserResource(userId, parentId, type, status);
        if (resourcesDtoList != null) {
            for (int i = 0; i < resourcesDtoList.size(); i++) {
                resourcesDtoList.get(i).setChildren(
                        setChildren(userId, type, resourcesDtoList.get(i).getId(), status));
            }
        }
        return resourcesDtoList;
    }

    /**
     * 计算该节点下 - 所有有子节点
     *
     * @param userId
     * @param type
     * @param pid
     * @param status
     * @return
     */
    public Integer countAllChild(Long userId, Long pid, Integer type, Integer status) {
        int count = 0;
        List<ResourceDto> sysResourcesDtoList = selectUserResource(userId, pid, type, status);
        count = count + sysResourcesDtoList.size();
        if (sysResourcesDtoList != null) {
            for (int i = 0; i < sysResourcesDtoList.size(); i++) {
                count = count + countAllChild(pid, userId, type, status);
            }
        }
        return count;
    }

    /**
     * 计算该节点下 - 一级子节点
     *
     * @param userId
     * @param type
     * @param pid
     * @param status
     * @return
     */
    public Integer countChild(Long userId, Long pid, Integer type, Integer status) {
        Map<String, Object> params = new HashMap<String, Object>();
        String resourceIds = "";
        if (userId != null) {
            resourceIds = getResourceIds(userId, resourceIds);
        }
        params.put("type", type);
        params.put("pid", pid);
        params.put("status", status);
        params.put("resourceIds", resourceIds);
        return resourceService.countByIds(params);
    }

    private String getResourceIds(Long userId, String resourceIds) {
        UserRoleDto userRoleDto = new UserRoleDto();
        userRoleDto.setUserId(userId);
        List<UserRoleDto> userRoleDtoList = userRoleService.selectList(userRoleDto);

        String roleIds = "";
        for (UserRoleDto userRole : userRoleDtoList) {
            if (userRole.getRoleId() != null) {
                roleIds = roleIds + userRole.getRoleId() + ",";
            }
        }
        roleIds = roleIds.substring(0, roleIds.length() - 1);
        List<RoleResourcePermissionDto> roleResourcePermissionDtoList = roleResourcePermissionService.selectListByRoleIds(roleIds);

        for (RoleResourcePermissionDto rrpDto : roleResourcePermissionDtoList) {
            if (rrpDto.getResourceId() != null) {
                resourceIds = resourceIds + rrpDto.getResourceId() + ",";
            }
        }
        resourceIds = resourceIds.substring(0, resourceIds.length() - 1);
        return resourceIds;
    }
}
